var $;
var table;
var layer;
var form;
layui.use(['table','element','layer'],function(){
    $=layui.jquery;
    table=layui.table;
    layer=layui.layer;
    form=layui.form;
    var course=$("#courseid").val();
    SeCourseView(course);

    //获取当前登陆人员的信息
    var create=localStorage.getItem("user");
    create=JSON.parse(create);//将获取json字符串转json对象
    $("#sendUserName").val(create.true_name);
    $("#sendUserAccount").val(create.account);

    function SeCourseView(id){
        $.ajax({
            url:"/SyteacherSelController/SeCourseView",
            type:"post",
            dataType:"json",
            data:{"courseid":id},
            success:function(data){
                $("#courseName").val(data.data.courseName);

                var date=data.data.createDate
                date = date.slice(0, date.indexOf("."));
                date = date.replaceAll('T',' ');
                $("#createDate").val(date);
                $("#createName").val(data.data.userName);
                $("#courseNprice").val(data.data.courseNprice);
                $("#coursePrimary").val(data.data.coursePrimary);
                $("#courseCatory").val(data.data.className);
                $("#likNum").val(data.data.likeNum);
                $("#collectNum").val(data.data.collectNum);
                $("#shareNum").val(data.data.saleNum);
                $("#courseIntro").val(data.data.courseIntro);
                $("#remarks").val(data.data.remarks);
                $("#imgpid").html("<img src='http://182.92.209.121:/"+data.data.url+"' style='width: 250px;'>");

                //预览视频
                $("#video_id").html("  <source src='http://182.92.209.121:/"+data.data.courseUrl+"' type=\"video/mp4\"  id=\"source_id\"/>\n" +
                    "                            <object data='http://182.92.209.121:/"+data.data.courseUrl+"' width=\"320\" height=\"240\" id=\"object_id\">\n" +
                    "                                <embed src='http://182.92.209.121:/"+data.data.courseUrl+"' width=\"320\" height=\"240\" id=\"embed_id\"/>\n" +
                    "                            </object>")
            }
        })
    }


    //评论区
    getcomment()



});


/*日期转换*/
function settime(da){
    var date=new Date(da);
    var year=date.getFullYear();
    var month=date.getMonth()+1;
    var day=date.getDate();
    var hours=date.getHours();
    var mm=date.getMinutes();
    var s=date.getSeconds();
    return year+'-'+add(month)+'-'+add(day)+' '+add(hours)+':'+add(mm)+':'+add(s);
    //return year+'-'+month+'-'+day+' '+hours+':'+mm+':'+s;
};
function add(time){//给非整数添0
    return time<10?'0'+time:time;
};


var liminum=0;

function getcomment() {
    var id=$("#courseid").val();
    var com="";
    var type=2;
    $.post("/SyOperationController/selCommentInfo", {"id":id,"type":type}, function (res) {
        if (res.code>-1) {
            if(res.data.length!=0){
                liminum=res.data.length;

                var arr=[];
                for(var i=0;i<res.data.length;i++){

                    var date=res.data[i].commentDate
                    date = date.slice(0, date.indexOf("."));
                    date = date.replaceAll('T',' ');
                    arr.push(date);

                    if(res.data[i].userImg!=null){
                        com+="<li class='ulli'><div class='lidiv'>" +
                            "<div class='lidiv1'><div class='liImgdiv'><img class='UserImg' src='"+res.data[i].userImg+"'/></div></div>" +
                            "<div class='licondiv'><p class='userNane'>"+res.data[i].userName+"</p>" +
                            "<p class='comp'>"+res.data[i].content+"</p>" +
                            "<p class='actp'>"+settime(arr[i])+ "" +
                            "<span class='pspupd' onclick='updComment(\""+res.data[i].id+"\",\""+res.data[i].content+"\")'>隐藏</span>" +
                            "<span class='pspdel' onclick='delComment(\""+res.data[i].id+"\",\""+res.data[i].content+"\")'>删除</span></p></div>" +
                            "<input class='comid' value='"+res.data[i].id+"' type='hidden' name='comid'></div></li>";
                    }else {
                        com+="<li class='ulli'><div class='lidiv'>" +
                            "<div class='lidiv1'><div class='liImgdiv'><i class='layui-icon' class='li_i'>&#xe612;</i></div></div>" +
                            "<div class='licondiv'><p class='userNane'>"+res.data[i].userName+"</p>" +
                            "<p class='comp'>"+res.data[i].content+"</p>" +
                            "<p class='actp'>"+settime(arr[i])+
                            "<span class='pspupd' onclick='updComment(\""+res.data[i].id+"\",\""+res.data[i].content+"\")'>隐藏</span>" +
                            "<span class='pspdel' onclick='delComment(\""+res.data[i].id+"\",\""+res.data[i].content+"\")'>删除</span></p></div>" +
                            "<input class='comid' value='"+res.data[i].id+"' type='hidden' name='comid'></div></li>";
                    }
                }


                $("#commentUl").html(com);
                $('#nocomDiv').css("display","block");
            }else{
                $('#nocomDiv').css("display","block");
            }
        } else {
            $('#nocomDiv').css("display","block");
        }
    });
}

//评论状态修改
function updComment(id,content) {
    var status=2;
    layer.confirm("您确定要拦截`"+content+"`评论吗?",function (rel) {
        $.ajax({
            url: "/SyOperationController/updComment",
            type: "post",
            dataType: "json",
            data:{"id":id,"status":status},
            success: function (data) {
                if(data.code==0){
                    layer.msg("拦截成功",{icon:1});
                    getcomment();
                }else{
                    layer.msg("拦截失败",{icon:2});
                    getcomment();
                }
            }
        });
    },function (res) {
    });
}

//评论状态修改为删除
function delComment(id,content) {
    layer.confirm("您确定要删除`"+content+"`评论吗?",function (rel) {
        $.ajax({
            url: "/SyOperationController/updComment",
            type: "post",
            dataType: "json",
            data:{"id":id,"status":3},
            success: function (data) {
                if(data.code==0){
                    layer.msg("删除成功",{icon:1});
                    getcomment();
                }else{
                    layer.msg("删除失败",{icon:2});
                    getcomment();
                }
            }
        });
    },function (res) {

    });
}

//评论视频
function addCommentInfo(){
    var vals = "";
    var content = $("#textsId").val();
    if(!content){
        layer.msg("内容不能为空",{icon:5});
    }else{
        var create=localStorage.getItem("user");
        create=JSON.parse(create);//将获取json字符串转json对象
        var userId = create.user_id;
        var commodityId=$("#courseid").val();
        vals+="&userId="+userId;
        vals+="&commodityId="+commodityId;
        vals+="&content="+content;
        vals+="&type="+2;
        $.post("/SyOperationController/addCommentInfo", vals, function (res) {
            if(res.code>-1){
                layer.msg("评论成功",{icon:1});
                getcomment();
                $("#textsId").val('');
            }else{
                layer.msg("评论失败",{icon:2});
                getcomment();
            }
        });
    }
}



